package org.dromara.genealogy.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.genealogy.domain.vo.GeNameChildrenVo;
import org.dromara.genealogy.domain.bo.GeNameChildrenBo;
import org.dromara.genealogy.service.IGeNameChildrenService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 子女信息
 *
 * @author qianjunmo
 * @date 2025-09-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/genealogy/nameChildren")
public class GeNameChildrenController extends BaseController {

    private final IGeNameChildrenService geNameChildrenService;

    /**
     * 查询子女信息列表
     */
    @SaCheckPermission("genealogy:nameChildren:list")
    @GetMapping("/list")
    public TableDataInfo<GeNameChildrenVo> list(GeNameChildrenBo bo, PageQuery pageQuery) {
        return geNameChildrenService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出子女信息列表
     */
    @SaCheckPermission("genealogy:nameChildren:export")
    @Log(title = "子女信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GeNameChildrenBo bo, HttpServletResponse response) {
        List<GeNameChildrenVo> list = geNameChildrenService.queryList(bo);
        ExcelUtil.exportExcel(list, "子女信息", GeNameChildrenVo.class, response);
    }

    /**
     * 获取子女信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("genealogy:nameChildren:query")
    @GetMapping("/{id}")
    public R<GeNameChildrenVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(geNameChildrenService.queryById(id));
    }

    /**
     * 新增子女信息
     */
    @SaCheckPermission("genealogy:nameChildren:add")
    @Log(title = "子女信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GeNameChildrenBo bo) {
        return toAjax(geNameChildrenService.insertByBo(bo));
    }

    /**
     * 修改子女信息
     */
    @SaCheckPermission("genealogy:nameChildren:edit")
    @Log(title = "子女信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GeNameChildrenBo bo) {
        return toAjax(geNameChildrenService.updateByBo(bo));
    }

    /**
     * 删除子女信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("genealogy:nameChildren:remove")
    @Log(title = "子女信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(geNameChildrenService.deleteWithValidByIds(List.of(ids), true));
    }
}
